<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;

class ThingYuRuiController extends BaseController
{
  public function __construct()
  {
    $this->log = new Logger('ThingPushYuRui');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/ThingPushYuRui.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 电信那边推送过来的数据 
  public function pushOrderStatus(Request $Request)
  {
    // 刘志恒 让取消发货等状态接受
    return json_encode(['responseCode' => 0, 'result' => '接受成功']);
    $getData = file_get_contents('php://input');
    $this->log->info('input' . ($getData));
    // return '接受成功';
    // var_dump(1111111111);
    // $getData = 'outer_id=4971879589548764751&plan_mobile_produced=&status=-460&iccid=&sign=f15007f6b2719c4586136d7d73b758b4&tracking_number=&tracking_company=&re
    // ason=2022-08-292022-08-29+00%3A18%3A27%E4%B8%8B%E5%8D%95%E7%94%9F%E4%BA%A7%E8%BF%94%E5%9B%9E%E5%A4%B1%E8%B4%A5%3A%E7%B3%BB%E7%BB%9F%E6%92%A4%E5%8D%95%EF%BC%88%E5%85%A5%E7%BD%91%E8%B5%84%E6%A0%B
    // C%E6%A0%A1%E9%AA%8C%E5%A4%B1%E8%B4%A5%EF%BC%8C%E7%9C%81%E5%86%85%E4%B8%9A%E5%8A%A1%E9%99%90%E5%88%B6%EF%BC%89&is_activated=0&activated_at=&is_recharged=0&recharged_at=';
    // $getData = 'outer_id=HK_20220829102232911348186809&plan_mobile_produced=17752397462&status=550&iccid=&sign=861e354a02a66bc2b4ca315f5624350a&tracking_number=JDV008027944720&tracking_company=%E4%BA%AC%E4%B8%9C%E9%85%8D%E9%80%81&reason=2022-08-29%E6%93%8D%E4%BD%9C%E6%88%90%E5%8A%9F&is_activated=0&activated_at=&is_recharged=0&recharged_at=';
    // $getData = '{"order_no":"SN_202207201608058395","iccid":"001840750","state":"1","express_name":"京东快递","express_no":"JDVE05493628845","orderState":2,"stateDate":"2022-08-19 15:04:34"}';
    // 数据处理  
    if (empty($getData)) {
      return json_encode(['responseCode' => 103, 'result' => '参数为空']);
      return 'error';
    }
    if (!empty($getData) && is_string($getData)) {
      $getData = json_decode($getData, true);
    }
    if (empty($getData)) {
      return json_encode(['responseCode' => 103, 'result' => '参数为空']);
      return 'error';
    }
    $statusDatas = [
      0 => '提交中',
      1 => '提交上游',
      // 2 => '拒收单',
      10 => '审核中',
      11 => '待发货',
      20 => '已发货',
      30 => '已签收',
      40 => '已激活',
      50 => '异常单',
      51 => '取消单',
      60 => '失败单'
    ];
    $state = [
      '0' => '未注册',
      '1' => '已激活',
      '2' => '已注册',
    ];
    $orderState = [
      '0' => '待处理',
      '1' => '待用户签收/已发货',
      '3' => '订单取消',
      '2' => '已完成',
    ];
    $returnStatusArr = array(
      '0' => 1,
      '1' => 20,
      '3' => 51,
      '2' => 51,
      '50' => 40
    );
    // 
    if (!empty($getData['state']) && $getData['state'] == 1) {
      $getData['orderState'] = 50;
    }
    // 将数据格式 修改成固定的数据格式
    $editData = [
      'status' => $returnStatusArr[$getData['orderState']],
      'express_company' => !empty($getData['express_name']) ? $getData['express_name'] : '',
      'express_number' => !empty($getData['express_no']) ? $getData['express_no'] : '',
      'three_msg' => !empty($getData['reason']) ? $getData['reason'] : '',
      'iccid' => !empty($getData['iccid']) ? $getData['iccid'] : '',
      'activat_at' => !empty($getData['active_time']) ? $getData['active_time'] : '',
    ];
    $where = ['orderno' => $getData['order_no']];
    $model = new MainOrderRepostition();
    $res = $model->followOrderStatus($where, $editData);
    $this->log->info('通知参数' . json_encode($getData) . '返回参数' . json_encode($res));
    if (!empty($res['code']) && $res['code'] == 200) {
      return json_encode(['responseCode' => 0, 'result' => '接受成功']);
      return 'success';
    }
    return json_encode(['responseCode' => 103, 'result' => '操作失败']);
    return 'error';
  }
}
